#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Text;
#endregion

namespace Thn.Communication.Teleporting.Connectors.WebService
{
    /// <summary>
    ///  Summary description for Convert Message.
    /// </summary>
    public class MessageConversion
    {


        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public MessageConversion()
        { }
        #endregion

        #region Int To ByteArray
        /// <summary>
        /// Convert int To byte[]
        /// </summary>
        /// <param name="message">Int</param>
        /// <returns></returns>
        public static byte[] IntToByteArray(int message)
        {
            byte[] result;
            result = BitConverter.GetBytes(message);
            return result;
        }
        #endregion


        #region ByteArrayToInt
        /// <summary>
        /// Convert byte[] to int
        /// </summary>
        /// <param name="message">byte[]</param>
        /// <returns></returns>
        public static int ByteArrayToInt(byte[] message)
        {
            int result;
            result = BitConverter.ToInt32(message, 0);
            return result;
        }
        #endregion

        #region BoolToByteArray
        /// <summary>
        /// Convert bool To byte[]
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static byte[] BoolToByteArray(bool message)
        {
            byte[] result;
            result = BitConverter.GetBytes(message);
            return result;
        }
        #endregion

        #region ByteArrayToBool
        /// <summary>
        /// Convert byte[] To bool
        /// </summary>
        /// <param name="message">byte[]</param>
        /// <returns></returns>
        public static bool ByteArrayToBool(byte[] message)
        {
            bool result;
            result = BitConverter.ToBoolean(message, 0);
            return result;

        }
        #endregion

        #region StringToByteArray
        /// <summary>
        /// Convert string To byte[]
        /// </summary>
        /// <param name="message">string</param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string message)
        {
            byte[] result;
            result = Encoding.ASCII.GetBytes(message);
            return result;
        }
        #endregion

        #region ByteArrayToString
        /// <summary>
        /// Convert byte[] To string
        /// </summary>
        /// <param name="message">byte[]</param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] message)
        {
            string result;
            result = Encoding.ASCII.GetString(message);
            return result;

        }
        #endregion

        #region StringUTF8ToByteArray
        /// <summary>
        /// Convert string UTF8 To byte[]
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static byte[] StringUTF8ToByteArray(string message)
        {
            byte[] result;
            result = Encoding.UTF8.GetBytes(message);
            return result;

        }
        #endregion

        #region ByteArrayToStringUTF8
        /// <summary>
        /// Convert byte[] To string UTF8
        /// </summary>
        /// <param name="message">byte[]</param>
        /// <returns></returns>
        public static string ByteArrayToStringUTF8(byte[] message)
        {
            string result;
            result = Encoding.UTF8.GetString(message);
            return result;
        }
        #endregion

        #region CombineByteArray
        /// <summary>
        /// Combine byte[] and byte[]
        /// </summary>
        /// <param name="arrByte1">byte[]</param>
        /// <param name="arrByte2">byte[]</param>
        /// <returns></returns>
        public static byte[] CombineByteArray(byte[] arrByte1, byte[] arrByte2)
        {
            byte[] lArrByte = new byte[arrByte1.Length + arrByte2.Length];
            //lay day 1
            for (int i = 0; i < arrByte1.Length; i++)
                lArrByte[i] = arrByte1[i];
            //lay them day 2
            for (int i = 0; i < arrByte2.Length; i++)
                lArrByte[arrByte1.Length + i] = arrByte2[i];
            //
            return lArrByte;
        }
        #endregion

    }
}
